package com.flexcloudsdk.prospectivesearch;


import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;

import com.flexcloudsdk.prospectivesearch.CloudSubscription;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.prospectivesearch.FieldType;
import com.google.appengine.api.prospectivesearch.ProspectiveSearchServiceFactory;
import com.google.appengine.api.prospectivesearch.ProspectiveSearchService;
import com.google.appengine.api.prospectivesearch.Subscription;

public class CloudSearchService {
	private static final Logger log = Logger.getLogger(CloudSearchService.class.getName());
	private String sourceMethod;
	private String sourceClass;
	private ProspectiveSearchService service;
	public CloudSearchService() {
		log.info("CloudUser has been created by BlazeDS!");
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		sourceClass = CloudSearchService.class.getName();
		service = ProspectiveSearchServiceFactory.getProspectiveSearchService();
	}
	
	public void getDocument(HttpServletRequest req) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);
		service.getDocument(req);
	}
	
	public CloudSubscription getSubscription(String topic, String subId) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);
		CloudSubscription sub = new CloudSubscription();
		sub.setSubscription( service.getSubscription(topic, subId) );
		return sub;
	}
	
	public List<Subscription> listSubscriptions(String topic) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);
       	return service.listSubscriptions(topic);
	}
	
	public List<Subscription> listSubscriptions(String topic, String subIdStart, int maxResults, long expiresBefore) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);
		return service.listSubscriptions(topic, subIdStart, maxResults, expiresBefore);
	}
	
	public List<String> listTopics(String topicStart, long maxResults) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);
		return service.listTopics(topicStart, maxResults);
	}
	
	public void match(Entity document, String topic) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);
		service.match(document, topic);
	}

	public void match(Entity document, String topic, String resultKey) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);
		service.match(document, topic, resultKey);
	}
	
	public void match(Entity document, String topic, String resultKey, String resultRelativeUrl, String resultTaskQueueName, int resultBatchSize, boolean resultReturnDocument) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);
		service.match(document, topic, resultKey, resultRelativeUrl, resultTaskQueueName, resultBatchSize, resultReturnDocument);
	}
	
	public void subscribe(String topic, String subId, long leaseDurationSeconds, String query, Map<String, FieldType> schema) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);
		service.subscribe(topic, subId, leaseDurationSeconds, query, schema);
	}
	
	@Override
	public String toString() {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);
		return service.toString();
	}
	
	public void unsubscribe(String topic, String subId) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);
		service.unsubscribe(topic, subId);
	}
	
}
